Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): Make the available concat alignment strategies more generic #20644

Merged
merged 2 commits into from
Jan 28, 2025

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Jan 9, 2025

Closes #20637.

Simple extension to allow for more generic align options in concat (the concat strategy for "align" is directly associated with the "how" join method, so we can easily use join methods other than "full" - this can result in significant speedups if we know that "align_left" is sufficient/appropriate for our target frames 🚀)

  • Extends concat "how" parameters with "align inner", "align_left", "align_right", and "align_full" (which is equivalent to the existing/default "align", but more explicit).

  • Streamlines the internal concat/align code to use coalesce=True for all join types; no longer have to manually identify/drop the uncoalesced common join cols. Should get a (minor) speedup from this too.

Example

import polars as pl

df1 = pl.DataFrame({"id": [1, 2], "x": [3, 4]})
df2 = pl.DataFrame({"id": [2, 3], "y": [5, 6]})
df3 = pl.DataFrame({"id": [1, 3], "z": [7, 8]})

pl.concat([df1, df2, df3], how="align_full")
# shape: (3, 4)
# ┌─────┬──────┬──────┬──────┐
# │ id  ┆ x    ┆ y    ┆ z    │
# │ --- ┆ ---  ┆ ---  ┆ ---  │
# │ i64 ┆ i64  ┆ i64  ┆ i64  │
# ╞═════╪══════╪══════╪══════╡
# │ 1   ┆ 3    ┆ null ┆ 7    │
# │ 2   ┆ 4    ┆ 5    ┆ null │
# │ 3   ┆ null ┆ 6    ┆ 8    │
# └─────┴──────┴──────┴──────┘

pl.concat([df1, df2, df3], how="align_left")
# shape: (2, 4)
# ┌─────┬─────┬──────┬──────┐
# │ id  ┆ x   ┆ y    ┆ z    │
# │ --- ┆ --- ┆ ---  ┆ ---  │
# │ i64 ┆ i64 ┆ i64  ┆ i64  │
# ╞═════╪═════╪══════╪══════╡
# │ 1   ┆ 3   ┆ null ┆ 7    │
# │ 2   ┆ 4   ┆ 5    ┆ null │
# └─────┴─────┴──────┴──────┘

pl.concat([df1, df2, df3], how="align_right")
# shape: (2, 4)
# ┌─────┬──────┬──────┬─────┐
# │ id  ┆ x    ┆ y    ┆ z   │
# │ --- ┆ ---  ┆ ---  ┆ --- │
# │ i64 ┆ i64  ┆ i64  ┆ i64 │
# ╞═════╪══════╪══════╪═════╡
# │ 1   ┆ null ┆ null ┆ 7   │
# │ 3   ┆ null ┆ 6    ┆ 8   │
# └─────┴──────┴──────┴─────┘

pl.concat([df1, df2, df3], how="align_inner")
# shape: (0, 4)
# ┌─────┬─────┬─────┬─────┐
# │ id  ┆ x   ┆ y   ┆ z   │
# │ --- ┆ --- ┆ --- ┆ --- │
# │ i64 ┆ i64 ┆ i64 ┆ i64 │
# ╞═════╪═════╪═════╪═════╡
# └─────┴─────┴─────┴─────┘

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Jan 9, 2025
Copy link

codecov bot commented Jan 9, 2025

Codecov Report

Attention: Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 79.02%. Comparing base (247f0b1) to head (440e847).
Report is 157 commits behind head on main.

Files with missing lines Patch % Lines
py-polars/polars/functions/eager.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #20644      +/-   ##
==========================================
- Coverage   79.03%   79.02%   -0.02%     
==========================================
  Files        1557     1557              
  Lines      220528   220554      +26     
  Branches     2510     2514       +4     
==========================================
- Hits       174303   174286      -17     
- Misses      45651    45695      +44     
+ Partials      574      573       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ritchie46 ritchie46 merged commit 704ff3f into pola-rs:main Jan 28, 2025
31 checks passed
@alexander-beedie alexander-beedie deleted the concat-generic-align branch January 28, 2025 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Add how="align_left" to pl.concat() for faster alignment
2 participants